package com.example;

/**
 * Created by Administrator on 2017/2/16.
 */
public class Leetcode_2 {

    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int more=0;
            ListNode l=new ListNode(0);
            ListNode p = l;
            while(l1!=null || l2!=null){
                int a = l1==null?0:l1.val;
                int b = l2==null?0:l2.val;
                int tmp= a+b+more;
                if(tmp/10==1){
                    l.next=new ListNode(tmp%10);
                    more=1;
                }else{
                    l.next=new ListNode(tmp);
                    more=0;
                }
                l=l.next;
                if(l1.next!=null)
                l1=l1.next;
                if(l2.next!=null)
                l2=l2.next;
            }
            return p.next;
        }
    }
    public class ListNode {
     int val;
     ListNode next;
     ListNode(int x) { val = x; }
  }
}
